package thn.glf.org.personalfinancial.model;

import java.util.ArrayList;

import thn.glf.org.personalfinancial.MoneyOfDayListActivity;
import thn.glf.org.personalfinancial.R;
import thn.glf.org.personalfinancial.objData.FinancialDay;
import thn.glf.org.personalfinancial.objData.FinancialMonth;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class FinancialMonthAdapter extends BaseExpandableListAdapter{
	private Context context;
	private ArrayList<FinancialMonth> lstFinancialMonth;
	private ArrayList<FinancialDay> lstFinancialDay;
	private FinancialDay financialDay;
	public FinancialMonthAdapter(Context mcontext,ArrayList<FinancialMonth>lst){
		this.context=mcontext;
		this.lstFinancialMonth=lst;
		financialDay=new FinancialDay();
		financialDay.setmContext(context);
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return this.financialDay.getListOfMonth(lstFinancialMonth.get(groupPosition));
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			   LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			   convertView = infalInflater.inflate(R.layout.lstitem, null);
			  }
		TextView textView=(TextView)convertView.findViewById(R.id.textView1);
		final String date=financialDay.getListOfMonth(this.lstFinancialMonth.get(groupPosition)).get(childPosition).getDays();
		textView.setText(date.substring(0,4)+"/"+date.substring(4, 6)+"/"+date.substring(6));
		textView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context, MoneyOfDayListActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("day",date );
				intent.putExtra("listday", bundle);
				context.startActivity(intent);
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.financialDay.getListOfMonth(lstFinancialMonth.get(groupPosition)).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return lstFinancialMonth.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return lstFinancialMonth.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			   LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			   convertView = inf.inflate(R.layout.group_heading_month, null);
			  }
		TextView textView=(TextView)convertView.findViewById(R.id.textView1);
		textView.setText("Tháng "+String.format("%02d",  lstFinancialMonth.get(groupPosition).getMonthID()));
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

}
